package resp

import (
	"com.codingq92/go-chat/global"
	"github.com/gin-gonic/gin"
	"net/http"
)

type Response struct {
	ErrorCode int    `json:"errorCode"` //0代表成功
	Message   string `json:"message"`   //错误信息
	Data      any    `json:"data"`      //数据
}

// Success 正确返回
func Success(c *gin.Context, data any) {
	c.JSON(http.StatusOK, Response{0, "ok", data})
}

// Fail 错误返回
func Fail(c *gin.Context, errorCode int, message string) {
	c.JSON(http.StatusOK, Response{errorCode, message, nil})
}

// FailByError 错误返回
func FailByError(c *gin.Context, error global.CustomError) {
	Fail(c, error.ErrorCode, error.ErrorMsg)
}

// BusinessFail 业务逻辑失败
func BusinessFail(c *gin.Context, msg string) {
	Fail(c, global.Errors.BusinessError.ErrorCode, msg)
}

// TokenFail token错误
func TokenFail(c *gin.Context) {
	FailByError(c, global.Errors.TokenError)
}
